package com.example.pcl.controller.admin;

import com.example.pcl.constant.MessageConstants;
import com.example.pcl.pojo.dto.UserDto;
import com.example.pcl.pojo.dto.UserQueryDto;
import com.example.pcl.result.Result;
import com.example.pcl.service.IUsersService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户表，存储所有用户信息 前端控制器
 * </p>
 *
 * @author xx
 * @since 2024-12-17
 */
@RestController("adminUsersController")
@RequestMapping("/admin/users")
@Slf4j
@RequiredArgsConstructor
@Tag(name = "管理端用户相关接口")
public class UsersController {

    private final IUsersService usersService;

    @GetMapping("/list")
    @Operation(summary = "分页查询用户列表")
    public Result<?> list(UserQueryDto queryDto) {
        log.info("管理端分页查询用户列表：{}", queryDto);
        return Result.success(usersService.pageList(queryDto), MessageConstants.OPERATION_SUCCESSFUL);
    }

    /**
     * 修改用户个人信息
     *
     * @param userDto 用户信息 DTO
     * @return 修改结果
     */
    @PutMapping("/update")
    @Operation(summary = "管理员修改用户个人信息")
    public Result<?> updateUserInfo(@RequestBody UserDto userDto) {
        log.info("管理员修改用户信息: {}", userDto);
        usersService.updateUserInfo(userDto);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }

    /**
     * 批量删除用户
     *
     * @param ids ids
     * @return 提示
     */
    @DeleteMapping("/deleteBatch")
    @Operation(summary = "批量删除用户")
    public Result<?> deleteBatch(@RequestParam List<Integer> ids) {
        log.info("管理员批量删除用户:{}", ids);
        usersService.deleteBatch(ids);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }

    /**
     * 添加用户
     *
     * @param userDto dto
     * @return 提示
     */
    @PostMapping("/add")
    @Operation(summary = "添加用户")
    public Result<?> add(@RequestBody UserDto userDto) {
        log.info("管理员添加用户:{}", userDto);
        usersService.addUser(userDto);
        return Result.success(MessageConstants.ADD_OK);
    }

    @GetMapping("/detail/{userid}")
    @Operation(summary = "获取用户详细信息")
    public Result<?> detail(@PathVariable Integer userid) {
        log.info("管理员获取用户详细信息:{}", userid);
        return Result.success(usersService.getById(userid), MessageConstants.SELECT_OK);
    }

    @PostMapping("/reset-password/{userId}")
    @Operation(summary = "重置密码")
    public Result<?> resetPassword(@PathVariable Integer userId) {
        log.info("管理员为用户修改密码：{}", userId);
        usersService.resetPassword(userId);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }
}